<template>
	<view class="page" :style="{ paddingBottom: currentMusicId != '' ? '100rpx' : '0' }">
		<navbar :showBack="false" :isDocument="true" bgc="#fff" iconColor="black" :showMenu="true">
			<view class="search">
				<u-search :placeholder="defaultSearch" bg-color="#f7f7f7" height="60" :disabled="true" @click="toSearch"
				 :show-action="true" @custom="toSearch"></u-search>
			</view>
		</navbar>

		<view class="banner">
			<u-swiper :list="banner" name="pic"></u-swiper>
		</view>

		<view class="icons">
			<view class="iconItem" v-for="(item,index) in iconList" :key="index" @click="iconsClick(item.page)">
				<view class="iconBox">
					<image :src="item.icon" mode="widthFix"></image>
				</view>
				<text>{{item.title}}</text>
			</view>
		</view>

		<view class="recommendSongList">
			<RecommendCard title="推荐歌单" :list="recommendSongList" :showShadow="true" itemPlayCountKey="playcount"></RecommendCard>
		</view>
		
		<view class="rankCard">
			<RankCard title="排行榜" :list="musicTopList"></RankCard>
		</view>

		<view class="recommendMv">
			<RecommendCard title="推荐MV" :list="recommendMV"></RecommendCard>
		</view>
		
		<view class="recommendFM">
			<RecommendCard title="推荐电台" :list="recommendFM"></RecommendCard>
		</view>
		<playMusicBar></playMusicBar>
	</view>
</template>

<script>
	import Navbar from '@/components/navbar.vue'
	import { mapState, mapMutations } from 'vuex'
	import RecommendCard from '@/components/recommendCard.vue'
	import RankCard from '@/components/rankCard.vue'
	import {
		getDefaultSearch,
		getFoundBanner,
		getRecommendSongList,
		getRecommendMV,
		getRecommendFM,
		getMusicTopList
	} from '@/api/found.js'
	export default {
		components: {
			Navbar,
			RecommendCard,
			RankCard
		},
		data() {
			return {
				// 默认的搜索关键字
				defaultSearch: '',
				// 轮播图数据
				banner: [],
				// 入口图标列表
				iconList: [{
						icon: '/static/images/calendar.png',
						title: '每日推荐',
						page: '/pages/music/dailyRecommend'
					},
					{
						icon: '/static/images/FM.png',
						title: '私人FM',
						page: '/pages/music/FM/FM'
					},
					{
						icon: '/static/images/songList.png',
						title: '歌单',
						page: '/pages/music/songList/songList'
					},
					{
						icon: '/static/images/rank-r.png',
						title: '排行榜',
						page: '/pages/found/rank'
					},
					{
						icon: '/static/images/album.png',
						title: '数字专辑',
						page: '/components/webView?url=https://music.163.com/v/m/album/poly/detail'
					}
				],
				// 每日推荐歌单数据
				recommendSongList: [],
				// 每日推荐MV数据
				recommendMV: [],
				// 每日推荐电台数据
				recommendFM: [],
				// 音乐排行榜数据
				musicTopList: [],
				
			}
		},
		onLoad() {
			// 获取轮播图
			getFoundBanner().then(res => this.banner = res.data.banners)
			// 获取每日推荐歌单
			getRecommendSongList().then(res => this.recommendSongList = res.data.recommend)
			// 获取每日推荐MV
			getRecommendMV().then(res => this.recommendMV = res.data.result)
			// 获取每日推荐电台
			getRecommendFM().then(res => this.recommendFM = res.data.result)
			// 获取音乐排行榜
			getMusicTopList().then(res => this.musicTopList = res.data.list.slice(0,5))
		},
		computed: {
			...mapState(['currentMusicId'])
		},
		onShow() {
			// 获取默认的搜索关键字
			const that = this
			getDefaultSearch().then(res => {
				that.defaultSearch = res.data.showKeyword
				that.changeDefaultSearch(res.data)
			})
			var a = setInterval(function() {
				getDefaultSearch().then(res => {
					that.defaultSearch = res.data.showKeyword
					that.changeDefaultSearch(res.data)
				})
			},60000)
		},
		methods: {
			...mapMutations(['changeDefaultSearch']),
			// 跳转搜索页面
			toSearch() {
				uni.navigateTo({
					url: '/pages/search/index',
				})
			},
			// 点击icon列表,跳转对应页面
			iconsClick(page) {
				uni.navigateTo({
					url: page,
					fail(err) {
						console.log(err)
					}
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	.search {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100%;
		height: 100%;

		/deep/ input {
			font-size: 24rpx !important;
		}
	}

	.banner {
		width: 750rpx;
		// height: 280rpx;
		padding: 20rpx;
		background-color: #fff;
	}

	.icons {
		display: flex;
		justify-content: space-around;
		align-items: center;
		width: 750rpx;
		height: 160rpx;
		background-color: #fff;
		border-bottom: 1rpx solid #eee;

		.iconItem {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			.iconBox {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 90rpx;
				height: 90rpx;
				border-radius: 45rpx;
				background-color: #fef0f0;

				image {
					width: 50%;
				}
			}

			text {
				margin-top: 10rpx;
				font-size: 26rpx;
				color: #000;
			}
		}
	}

	.recommendSongList {
		margin-bottom: 20rpx;
		background-color: #fff;
		border-bottom-left-radius: 20rpx;
		border-bottom-right-radius: 20rpx;
	}
	.recommendMv {
		margin-bottom: 20rpx;
	}
	.rankCard {
		margin-bottom: 20rpx;
	}
</style>
